package com.wanghua.spring.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JavaProperties {
    public static String getValue(String fileNamePath, String key) throws IOException {
        Properties props = new Properties();
        InputStream in = null;
        try {
            in = new FileInputStream(fileNamePath);
            // 如果将in改为下面的方法，必须要将.Properties文件和此class类文件放在同一个包中
            //in = propertiesTools.class.getResourceAsStream(fileNamePath);
            props.load(in);
            String value = props.getProperty(key);
            // 有乱码时要进行重新编码
            // new String(props.getProperty("name").getBytes("ISO-8859-1"), "GBK");
            return value;

        } catch (IOException e) {
            e.printStackTrace();
            throw new IOException(e.getMessage());
        } finally {
            if (null != in)
                in.close();
        }
    }

    public static void main(String[] args) throws IOException {
        //注意：JavaEE调试时不能用 “src/jdbc.properties” 路径。
        System.out.println(JavaProperties.getValue("src/jdbc.properties", "mail.smtp.from"));
    }
}
